package com.help.knowhelps.commond;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.help.knowhelps.ui.KidsModeContext;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;



public class Utils {
	/** 
     * 根据手机的分辨率�? px(像素) 的单�? 转成�? dp 
     */  
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  
    /** 
     * 根据手机的分辨率�? dp 的单�? 转成�? px(像素) 
     */  
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    } 

	public static Bitmap convertToBitmap(String path, int w, int h) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		// 设置为ture只获取图片大小
		opts.inJustDecodeBounds = true;
		opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
		// 返回为空
		BitmapFactory.decodeFile(path, opts);
		int width = opts.outWidth;
		int height = opts.outHeight;
		float scaleWidth = 0.f, scaleHeight = 0.f;
		if (width > w || height > h) {
			// 缩放
			scaleWidth = ((float) width) / w;
			scaleHeight = ((float) height) / h;
		}
		opts.inJustDecodeBounds = false;
		float scale = Math.max(scaleWidth, scaleHeight);
		opts.inSampleSize = (int) scale;
		WeakReference<Bitmap> weak = new WeakReference<Bitmap>(
				BitmapFactory.decodeFile(path, opts));
		return Bitmap.createScaledBitmap(weak.get(), w, h, true);
	}
	/**
	 * 异或算法进行加密
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] encryptStream(byte[] data) {
		final byte KEY = 0x6e;
		if (data == null || data.length == 0) {
			return data;
		}
		for (int i = 0; i < data.length; i++) {
			data[i] ^= KEY;
		}

		return data;
	}
	/**
	 * 异或算法进行加密
	 * 
	 * @param oneByte
	 * @return
	 */
	public static int encryptStream(int oneByte) {
		final byte KEY = 0x6e;
		return (oneByte ^ KEY);
	}
	 /**
     * 保存图片到磁盘，并返回全路径
     * @param context
     * @param bm
     * @return
     */
    public static String saveBitmapToDisk(final Context context, Bitmap bm){
		String returnString = null;
        BufferedOutputStream os = null;
        
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
        
		String path = KidsModeContext.getFileCache(context).getDir() + File.separator + dateFormat.format(date) + ".png";;
        try {
            File file = new File(path);
            int end = path.lastIndexOf(File.separator);
            String _filePath = path.substring(0, end);
            File filePath = new File(_filePath);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            file.createNewFile();
            os = new BufferedOutputStream(new FileOutputStream(file));
            bm.compress(Bitmap.CompressFormat.PNG, 100, os);
            returnString = path;
        } catch (Exception e){
        	e.printStackTrace();
        	returnString = "";
        }
        finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    returnString = "";
                }
            }
        }
        return returnString;
	}
}
